package com.dog.basics.service.common.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dog.basics.bean.dto.admin.common.NettyTreatyAdminDto;
import com.dog.basics.bean.dto.netty.NettyCommonDto;
import com.dog.basics.bean.model.common.DogAppVersionInfo;
import com.dog.basics.bean.model.mtkj.MtDeviceLock;
import com.dog.basics.common.util.RedisUtil;
import com.dog.basics.common.util.SpringContextUtils;
import com.dog.basics.service.common.IDogAppVersionInfoService;
import com.dog.basics.service.common.NettyTreatyService;
import com.dog.basics.service.mtkj.IMtDeviceLockService;
import lombok.AllArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description: 协议业务
 * @author: He
 * @date: Created in 2020/10/14 10:07
 * @version: 1.0
 * @modified By:
 */
@Service
@AllArgsConstructor
public class NettyTreatyServiceImpl implements NettyTreatyService {

    private final IMtDeviceLockService deviceLockService;

    private final IDogAppVersionInfoService appVersionInfoService;

    /**
     * @功能描述: 公共协议远程控制指令
     * @author He
     * @date 2020/10/14
     */
    @Override
    public Boolean sendTreatyInstructions(NettyTreatyAdminDto dto) {
        // 查询设备是否存在
        List<MtDeviceLock> deviceLocks = deviceLockService.getBaseMapper().selectBatchIds(dto.getDevIds());
        Validator.validateTrue(CollUtil.isNotEmpty(deviceLocks), "设备信息不存在");
        // 判断是什么命令指令
        NettyCommonDto build = null;
        switch (dto.getMsgType()) {
            case 5:
                // 远程在线升级
                NettyCommonDto.NettyCommonDtoBuilder nettyCommonDtoBuilder = NettyCommonDto.builder().msgType(dto.getMsgType());
                // 判断不同升级执行
                if (ObjectUtil.isNotNull(dto.getVersionId())) {
                    // 判断版本信息是否存在
                    DogAppVersionInfo versionInfo = appVersionInfoService.getById(dto.getVersionId());
                    Validator.validateNotNull(versionInfo, "选择固件信息不存在");
                    // 判断是否属于硬件固件版本信息
                    Validator.validateEqual(versionInfo.getAppType(), 3, "选择特定固件不属于硬件升级固件");
                    nettyCommonDtoBuilder.customVal(2);
                    nettyCommonDtoBuilder.firmwareVersion(versionInfo.getVersionCode());
                } else {
                    nettyCommonDtoBuilder.customVal(1);
                }
                build = nettyCommonDtoBuilder.build();
                break;
            case 6:
            case 7:
            case 8:
                // 远程重启 丶远程关机 丶远程自定义控制
                build = NettyCommonDto.builder().msgType(dto.getMsgType()).customVal(dto.getCustomVal()).build();
                break;
            default:
                Validator.validateTrue(Boolean.FALSE, "未知的指令操作");
                break;
        }
        // 循环执行发送信息
        for (int i = 0; i < deviceLocks.size(); i++) {
            build.setDevId(deviceLocks.get(i).getId());
            SpringContextUtils.getBean(RabbitTemplate.class).convertAndSend("EX-BUSINESS-MQ", "SERVICE-COMMON", build);
        }
        return Boolean.TRUE;
    }

    /**
     * @功能描述: 公共协议远程控制指令-硬件是否执行成功
     * @author He
     * @date 2020/10/14
     */
    @Override
    public Boolean sendTreatyInstructionsIsSuccess(NettyTreatyAdminDto dto) {
        Validator.validateNotNull(dto.getMsgType(), "命令类型不能为空");
        Validator.validateTrue(CollUtil.isNotEmpty(dto.getDevIds()), "查询设备为空");
        // 查询设备是否存在
        MtDeviceLock deviceLock = deviceLockService.getById(dto.getDevIds().get(0));
        Validator.validateNotNull(deviceLock, "设备信息不存在");
        // 判断查询什么控制命令执行情况
        switch (dto.getMsgType()) {
            case 3:
            case 5:
            case 6:
            case 7:
            case 8:
                // 判断查询执行成功否
                // 组装Redis - Key
                String sendKey = StrUtil.format("SEND-COMMAND:STATE:{}:{}", deviceLock.getId(), dto.getMsgType());
                // 判断是否有结果通知
                if (!RedisUtil.hasKey(sendKey)) {
                    return Boolean.FALSE;
                }
                // 查询结果并且删除
                Boolean dataReturn = RedisUtil.get(sendKey);
                RedisUtil.del(sendKey);
                return dataReturn;
            case 14:
                // 判断查询执行成功否
                // 组装Redis - Key
                String sendKey14 = StrUtil.format("SEND-COMMAND:STATE:{}:{}", deviceLock.getId(), dto.getMsgType());
                // 判断是否有结果通知
                if (!RedisUtil.hasKey(sendKey14)) {
                    return Boolean.FALSE;
                }
                // 查询结果并且删除
                Boolean dataReturn14 = RedisUtil.get(sendKey14);
                RedisUtil.del(sendKey14);
                return dataReturn14;
            default:
                Validator.validateTrue(Boolean.FALSE, "未知的指令情况查询");
                break;
        }
        return Boolean.TRUE;
    }
}